<?php

include_once(dirname(__FILE__) . "../../TMS_TEDxCairo_DATABASE_DAO/TaskDAO.class.php");
include_once(dirname(__FILE__) . "../../TMS_TEDxCairo_DATABASE_DAO/UserDAO.class.php");
include_once(dirname(__FILE__) . "../../TMS_TEDxCairo_ASSETS/Mailer.php");

$taskDAO = new TaskDAO();
$userDao = new UserDAO();
$mailer  = new Mailer();
if($_REQUEST['action']=="updateUserEval"){
    $taskDAO->updateUserTaskStatus($_REQUEST['evalId'], $_REQUEST['userId'], $_REQUEST['taskId']);
    exit;
}
if($_REQUEST['action']=="updateTaskStatus"){
    $taskDAO->updateTaskStatus($_REQUEST['statusId'], $_REQUEST['taskId']);
    exit;
}
if (isset($_REQUEST['typeId']) && trim($_REQUEST['typeId'] != '')) {
    if (isset($_REQUEST['action'])) {
        if ($_GET['action'] == 'new') {
            //
            // open new task
            //
            $users = $userDao->listAll();
            $taskType = $taskDAO->getTaskType($_GET['typeId']);
            $taskEvals = $taskDAO->listTaskEval();
            $taskStatuses = $taskDAO->listTaskStatuses();
            include_once 'views/newTask.html';
            exit;
        } elseif ($_POST['action'] == 'save') {
            //
            // save or Update new task
            //
            if (get_magic_quotes_gpc ())
                $desc = htmlspecialchars(stripslashes($_POST['description']));
            else
                $desc = htmlspecialchars($_POST['description']);

            if (isset($_POST['taskId'])) {
                $taskDAO->updateTask($_POST['taskName'], $_POST['taskPoints'], $_SESSION['CURRENT_USER']->id, $desc, $_POST['startDate'], $_POST['endDate'], $_REQUEST['typeId'],$_POST['evalId'] ,$_POST['taskId'],$_POST['taskStatusId']);
                $taskDAO->updateTaskAssignedUsers($_POST['taskId'], $_POST['userIds']);
                $status = "Task Updated ... :)";
            } else {
                $taskCheck = $taskDAO->checkTask($_POST['taskName'], $_POST['taskPoints'], $_SESSION['CURRENT_USER']->id, $desc, $_POST['startDate'], $_POST['endDate'], $_REQUEST['typeId']);
                // check if this task had inserted (ctrl + r effect :D )
                if (count($taskCheck) > 0)
                    $status = "Task Already exsist !!!  ... you are trying to reload the page ... please don't :)";
                else {
                    $taskId = $taskDAO->saveTask($_POST['taskName'], $_POST['taskPoints'], $_SESSION['CURRENT_USER']->id, $desc, $_POST['startDate'], $_POST['endDate'], $_REQUEST['typeId'],$_POST['evalId'],$_POST['taskStatusId'] );
                    $taskDAO->saveTaskAssignedUsers($taskId, $_POST['userIds']);
                    $assignedUsersMail = $userDao->getMails($_POST['userIds']);
                    $assignerMail = $_SESSION["CURRENT_USER"]->email;
                    $mailstatus = $mailer->SendTaskMail($assignedUsersMail, $_POST['taskName']." Added", $assignerMail, $_POST['description']);
                    $status = "Task Added ... :)";
                    if($mailstatus)
                        $status.=" and Mail Send To All Assigned users";
                    else
                        $status.=" BUT  Mail Not Send :(";
                }
            }
            include_once 'views/transactionStatus.html';
            exit;
        }
    } else {
        $tasks = $taskDAO->getTaskByTaskType($_GET['typeId']);
        $taskTitle = $_GET['name']." Tasks";
    }
} elseif (isset($_GET['taskId']) && trim($_GET["taskId"]) != "") {
    if ($_GET["action"] == "update") {
        // 
        // Update Task
        //
        $taskid = $_GET['taskId'];
        $updatedTask = $taskDAO->getTask($taskid);
        $users = $userDao->listAll();
        $taskStatuses = $taskDAO->listTaskStatuses();
        $taskEvals = $taskDAO->listTaskEval();
        include_once ("views/newTask.html");
        exit;
    } elseif ($_GET["action"]=="delete") {
        //
        // Delete Task
        //
        $taskDAO->deleteTask($_GET['taskId']);
        echo "Task Deleted ... :)";
        exit;
    }else {
        $task = $taskDAO->getTask($_GET['taskId']);
        include_once ("views/viewTask.html");
        exit;
    }
    //
    //  show task via opt
    //
} elseif (isset($_GET['opt']) && trim($_GET["opt"]) != "") {
    switch ($_GET['opt']) {
        case 'week':
            $tasks = $taskDAO->listWeekTasks();
            $taskTitle = "Up Coming Tasks";
            break;
        case 'all':
            $tasks = $taskDAO->listTasks();
            $taskTitle = "All Tasks";
            break;
        default :
            $tasks = $taskDAO->listTasksWithCriteria($_GET['opt']);
            $taskTitle = $_GET['opt'] . " Tasks";
    }
} else {
    //
    // simple list tasks :)
    //
    $tasks = $taskDAO->listTasks();
}
include_once ("views/{$_REQUEST["page"]}.html");
?>
